package com.company.nuwa.mybatis;//package com.company.mybatis;
//
//
//import cn.hutool.core.util.ReflectUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.cache.CacheKey;
//import org.apache.ibatis.executor.Executor;
//import org.apache.ibatis.mapping.BoundSql;
//import org.apache.ibatis.mapping.MappedStatement;
//import org.apache.ibatis.mapping.SqlSource;
//import org.apache.ibatis.plugin.*;
//import org.apache.ibatis.reflection.MetaObject;
//import org.apache.ibatis.session.ResultHandler;
//import org.apache.ibatis.session.RowBounds;
//import org.springframework.util.ObjectUtils;
//
///**
// * <p>description</p >
// *
// * @author Trump
// * @version 1.0
// * @date 2020/05/25 17:50
// */
//@Slf4j
//@Intercepts(
//    {
//       @Signature(
//            type = Executor.class,
//            method = "query",
//            args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
//    ), @Signature(
//            type = Executor.class,
//            method = "query",
//            args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
//    )}
//)
//public class MybatisInterceptor implements Interceptor {
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//
//        final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
//
//        Object parameter = invocation.getArgs()[1];
//        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
//
//        if (ObjectUtils.isEmpty(boundSql.getSql())) {
//            return null;
//        }
//        String originalSql = boundSql.getSql().trim();
//        originalSql = handleSQL(originalSql);
//        BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), originalSql, boundSql.getParameterMappings(), boundSql.getParameterObject());
//
//        if (ReflectUtil.getFieldValue(boundSql, "metaParameters") != null) {
//            MetaObject mo = (MetaObject) ReflectUtil.getFieldValue(boundSql, "metaParameters");
//            ReflectUtil.setFieldValue(newBoundSql, "metaParameters", mo);
//        }
//        MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
//
//        invocation.getArgs()[0] = newMs;
//
//        return invocation.proceed();
//    }
//
//    public String handleSQL(String originalSql){
//        return originalSql.replace("where","where merchant_id = 1001 and ");
//    }
//
//
//    @Override
//    public Object plugin(Object target) {
//        return Plugin.wrap(target, this);
//    }
//
//
//    private MappedStatement copyFromMappedStatement(MappedStatement ms,
//                                                    SqlSource newSqlSource) {
//        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(),
//                ms.getId(), newSqlSource, ms.getSqlCommandType());
//        builder.resource(ms.getResource());
//        builder.fetchSize(ms.getFetchSize());
//        builder.statementType(ms.getStatementType());
//        builder.keyGenerator(ms.getKeyGenerator());
//        if (ms.getKeyProperties() != null) {
//            for (String keyProperty : ms.getKeyProperties()) {
//                builder.keyProperty(keyProperty);
//            }
//        }
//        builder.timeout(ms.getTimeout());
//        builder.parameterMap(ms.getParameterMap());
//        builder.resultMaps(ms.getResultMaps());
//        builder.cache(ms.getCache());
//        return builder.build();
//    }
//
//    public static class BoundSqlSqlSource implements SqlSource {
//        BoundSql boundSql;
//
//        public BoundSqlSqlSource(BoundSql boundSql) {
//            this.boundSql = boundSql;
//        }
//
//        @Override
//        public BoundSql getBoundSql(Object parameterObject) {
//            return boundSql;
//        }
//    }
//}
